/*
 * Copyright 2001-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package code.buildit.plugin.fileconfig;

import code.buildit.plugin.configuration.Configuration;

public class JavaFile {
    private String packageName;
    private String fileName;

    public JavaFile(String packageName, String fileName) {
        this.packageName = packageName;
        this.fileName = fileName;
    }

    public JavaFile(String absoluteFileName) {
        this.packageName = absoluteFileName.substring(0, absoluteFileName.contains(".") ? absoluteFileName.lastIndexOf(".") : 0);
        this.fileName = absoluteFileName.substring(absoluteFileName.contains(".") ? absoluteFileName.lastIndexOf(".") + 1 : 0, absoluteFileName.length());
    }

    private JavaFileSourceBuilder buildable(Configuration configuration) {
        return new JavaFileSourceBuilder(this);
    }

    public String getAbsoluteFileName() {
        if (packageName.length() == 0)
            return fileName;
        return packageName + "." + fileName;
    }

    public String getPackageName() {
        return packageName;
    }

    public String getFileName() {
        return fileName;
    }

    public boolean isABuilderClass() {
        return fileName.endsWith(Configuration.BUILDER);
    }

    public String getSrcFileName(Configuration configuration) {
        return buildable(configuration).getSrcFileName();
    }

    public String getBuilderFileName(Configuration configuration) {
        return buildable(configuration).getBuilderFileName();
    }

    public String toggledName(Configuration configuration) {
        if (isABuilderClass()) {
            return getSrcFileName(configuration);
        }

        return getBuilderFileName(configuration);
    }


}
